using System;
using System.Collections.Generic;
using System.Linq;
using LF.Domain.Entities;
using LF.Domain.Helper;
using LF.Common;

namespace LF.Domain.Services.Iml
{
    public class ParamConfigManagerService : IParamConfigManagerService
    {
        public ParamConfig Get(Guid id)
        {
            return FreeSqlHelper.DbContext.Queryable<ParamConfig>()
                .Where(p => p.Id == id && !p.IsDelete)
                .First();
        }

        public List<ParamConfig> All(int index, int size, string name = null, string key = null)
        {
            var query = FreeSqlHelper.DbContext.Queryable<ParamConfig>()
                .Where(p => !p.IsDelete);

            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(p => p.Name.Contains(name));
            }

            if (!string.IsNullOrEmpty(key))
            {
                query = query.Where(p => p.Key.Contains(key));
            }

            return query.OrderByDescending(p => p.CreatTime)
                .Page(index, size)
                .ToList();
        }

        public long Count(string name = null, string key = null)
        {
            var query = FreeSqlHelper.DbContext.Queryable<ParamConfig>()
                .Where(p => !p.IsDelete);

            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(p => p.Name.Contains(name));
            }

            if (!string.IsNullOrEmpty(key))
            {
                query = query.Where(p => p.Key.Contains(key));
            }

            return query.Count();
        }

        public bool Insert(ParamConfig config)
        {
            var result = FreeSqlHelper.DbContext.Insert(config).ExecuteAffrows();
            return result > 0;
        }

        public bool Update(ParamConfig config)
        {
            var result = FreeSqlHelper.DbContext.Update<ParamConfig>(config.Id)
                .SetSource(config)
                .ExecuteAffrows();
            return result > 0;
        }

        public bool Delete(Guid id)
        {
            var result = FreeSqlHelper.DbContext.Update<ParamConfig>()
                .Set(p => p.IsDelete, true)
                .Where(p => p.Id == id)
                .ExecuteAffrows();
            return result > 0;
        }

        public ParamConfig GetByKey(string key)
        {
            return FreeSqlHelper.DbContext.Queryable<ParamConfig>()
                .Where(p => p.Key == key && !p.IsDelete)
                .First();
        }

        public void InitializeDefaultParams()
        {
            // 初始化企业微信机器人webhook_key参数
            var webhookKeyConfig = GetByKey("webhook_key");
            if (webhookKeyConfig == null)
            {
                var newConfig = new ParamConfig
                {
                    Id = Guid.NewGuid(),
                    Name = "企业微信机器人Webhook密钥",
                    Key = "webhook_key",
                    Value = "", // 默认为空，需要用户手动配置
                    Remark = "企业微信群机器人的Webhook密钥，用于发送消息通知",
                    CreatTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsDelete = false
                };
                Insert(newConfig);
                Console.WriteLine("已初始化企业微信机器人webhook_key参数配置");
            }

            // 初始化默认密码参数（如果不存在）
            var defaultPwdConfig = GetByKey("default_password");
            if (defaultPwdConfig == null)
            {
                var newConfig = new ParamConfig
                {
                    Id = Guid.NewGuid(),
                    Name = "默认密码",
                    Key = "default_password",
                    Value = "Aa12345",
                    Remark = "新用户的默认密码",
                    CreatTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsDelete = false
                };
                Insert(newConfig);
                Console.WriteLine("已初始化默认密码参数配置");
            }

            // 初始化qdrant_url参数（如果不存在）
            var qdrantConfig = GetByKey("qdrant_url");
            if (qdrantConfig == null)
            {
                var newConfig = new ParamConfig
                {
                    Id = Guid.NewGuid(),
                    Name = "Qdrant向量数据库地址",
                    Key = "qdrant_url",
                    Value = "http://localhost:6333",
                    Remark = "Qdrant向量数据库的连接地址",
                    CreatTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsDelete = false
                };
                Insert(newConfig);
                Console.WriteLine("已初始化Qdrant URL参数配置");
            }

            // 初始化服务报告地址参数（如果不存在）
            var serviceReportUrlConfig = GetByKey("service_report_url");
            if (serviceReportUrlConfig == null)
            {
                var newConfig = new ParamConfig
                {
                    Id = Guid.NewGuid(),
                    Name = "服务报告地址",
                    Key = "service_report_url",
                    Value = "http://localhost:5000/api/reports",
                    Remark = "服务报告的API地址，用于生成和获取服务报告",
                    CreatTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsDelete = false
                };
                Insert(newConfig);
                Console.WriteLine("已初始化服务报告地址参数配置");
            }
        }
    }
} 